package com.taxi.apiDriver.service;

import com.taxi.apiDriver.remote.ServiceDriverUserClient;
import com.taxi.apiDriver.remote.ServiceOrderClient;
import com.taxi.common.constant.CommonStatusEnum;
import com.taxi.common.constant.DriverCarConstants;
import com.taxi.common.constant.IdentityConstants;
import com.taxi.common.dto.DriverCarBindingRelationship;
import com.taxi.common.dto.DriverUserWorkStatus;
import com.taxi.common.dto.OrderInfo;
import com.taxi.common.dto.ResponseResult;
import com.taxi.common.request.DriverGrabRequest;
import com.taxi.common.request.OrderRequest;
import com.taxi.common.response.OrderDriverResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * @Author: Ruomeng
 * @Date: 2022/11/17
 * @Description : online-taxi
 * @Version: 1.0
 */
@Service
public class ApiDriverOrderInfoService {

    @Autowired
    ServiceOrderClient serviceOrderClient;

    @Autowired
    ServiceDriverUserClient serviceDriverUserClient;

    /**
     * 去接乘客
     * @param orderRequest
     * @return
     */
    public ResponseResult toPickUpPassenger(OrderRequest orderRequest){
        return serviceOrderClient.toPickUpPassenger(orderRequest);
    }

    /**
     * 司机到达乘客上车点
     * @param orderRequest
     * @return
     */
    public ResponseResult arrivedDeparture(OrderRequest orderRequest){
        return serviceOrderClient.arrivedDeparture(orderRequest);
    }

    /**
     * 司机接到乘客
     * @param orderRequest
     * @return
     */
    public ResponseResult pickUpPassenger(@RequestBody OrderRequest orderRequest){
        return  serviceOrderClient.pickUpPassenger(orderRequest);
    }

    /**
     * 乘客下车
     * @param orderRequest
     * @return
     */
    public ResponseResult passengerGetoff(OrderRequest orderRequest){
        return serviceOrderClient.passengerGetoff(orderRequest);
    }

    /**
     * 司机取消订单
     * @param orderId
     * @return
     */
    public ResponseResult cancel(Long orderId){
        return  serviceOrderClient.cancel(orderId, IdentityConstants.DRIVER_IDENTITY);
    }

    /**
     * 订单详情
     * @param orderId
     * @return
     */
    public ResponseResult<OrderInfo> detail(Long orderId){
        return serviceOrderClient.detail(orderId);
    }

    /**
     * 当前订单
     * @param phone
     * @param identity
     * @return
     */
    public ResponseResult<OrderInfo> currentOrder(String phone, String identity){
        return serviceOrderClient.current(phone,identity);
    }

    /**
     * 司机抢单
     * @param driverPhone
     * @param orderId
     * @param receiveOrderCarLongitude
     * @param receiveOrderCarLatitude
     * @return
     */
    public ResponseResult grab(String driverPhone, Long orderId, String receiveOrderCarLongitude, String receiveOrderCarLatitude){
        // 根据 司机的电话，查询车辆信息
        ResponseResult<DriverCarBindingRelationship> driverCarRelationShipResponseResult = serviceDriverUserClient.getDriverCarRelationShip(driverPhone);

        if (driverCarRelationShipResponseResult == null){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_CAR_BIND_NOT_EXISTS.getCode(),CommonStatusEnum.DRIVER_CAR_BIND_EXISTS.getValue());
        }
        DriverCarBindingRelationship driverCarBindingRelationship = driverCarRelationShipResponseResult.getData();
        Long carId = driverCarBindingRelationship.getCarId();
        //根据车辆查询可用司机信息
        ResponseResult<OrderDriverResponse> availableDriverResponseResult = serviceDriverUserClient.getAvailableDriver(carId);
        if (availableDriverResponseResult == null || availableDriverResponseResult.getData() == null){
            return ResponseResult.fail(CommonStatusEnum.CAR_NOT_EXISTS.getCode(),CommonStatusEnum.CAR_NOT_EXISTS.getValue());
        }
        OrderDriverResponse orderDriverResponse = availableDriverResponseResult.getData();
        Long driverId = orderDriverResponse.getDriverId();
        String licenseId = orderDriverResponse.getLicenseId();
        String vehicleNo = orderDriverResponse.getVehicleNo();
        String vehicleType = orderDriverResponse.getVehicleType();

        // 执行抢单动作
        DriverGrabRequest driverGrabRequest = new DriverGrabRequest();
        driverGrabRequest.setOrderId(orderId);
        driverGrabRequest.setDriverId(driverId);
        driverGrabRequest.setCarId(carId);
        driverGrabRequest.setDriverPhone(driverPhone);
        driverGrabRequest.setLicenseId(licenseId);
        driverGrabRequest.setVehicleNo(vehicleNo);
        driverGrabRequest.setVehicleType(vehicleType);
        driverGrabRequest.setReceiveOrderCarLatitude(receiveOrderCarLatitude);
        driverGrabRequest.setReceiveOrderCarLongitude(receiveOrderCarLongitude);

        ResponseResult responseResult = serviceOrderClient.driverGrab(driverGrabRequest);

        if (responseResult.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            return ResponseResult.fail(CommonStatusEnum.ORDER_UPDATE_ERROR.getCode(),CommonStatusEnum.ORDER_UPDATE_ERROR.getValue());
        }
        // 修改司机的工作状态
        DriverUserWorkStatus driverUserWorkStatus = new DriverUserWorkStatus();
        driverUserWorkStatus.setDriverId(driverId);
        driverUserWorkStatus.setWorkStatus(DriverCarConstants.DRIVER_WORK_STATUS_SERVING);

        responseResult = serviceDriverUserClient.changeWorkStatus(driverUserWorkStatus);
        if (responseResult.getCode() != CommonStatusEnum.SUCCESS.getCode()){
            return ResponseResult.fail(CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getCode(),CommonStatusEnum.DRIVER_STATUS_UPDATE_ERROR.getValue());
        }

        return ResponseResult.success();
    }

}
